package com.solution.admin.product.controller;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.product.entity.ProductSpecEntity;
import com.solution.admin.product.service.ProductSpecService;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 商品规格web控制层
 * @author lilp
 * @date 2017-08-29 14:31:56
 */
@RequestMapping("/productSpec")
@Controller
public class ProductSpecController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(ProductSpecController.class);

	@Autowired
	private ProductSpecService productSpecService;
	
	/**
	 * 跳转到选择商品模版页面
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2017年10月11日 下午3:16:24
	 */
	@RequestMapping("/toProductSpecSelector")
	public String toProductSpecSelector(HttpServletRequest request) {
		return "product/productSpec_selector";
	}
	
	/**
	 * 跳转商品规格查询页面
	 * @param ProductSpec 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lilp
 	 * @date 2017-08-29 14:31:56
	 */
	@RequestMapping("/toListProductSpecPage")
	public String toListProductSpecPage(HttpServletRequest request) {
		String tabIndex = request.getParameter("tabIndex");
		if(StringUtil.isEmpty(tabIndex)){
			request.setAttribute("tabIndex", "1");
		}else{
			request.setAttribute("tabIndex", tabIndex);
		}
		return "product/productSpec_list";
	}

	/**
	 * 分页查询商品规格列表
	 * @param productSpec 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lilp
	 * @throws UnsupportedEncodingException 
 	 * @date 2017-08-29 14:31:56
	 */
	@RequestMapping("/listProductSpecPage")
	@ResponseBody
	public JSONGrid listProductSpecPage(ProductSpecEntity productSpec, HttpServletRequest request) throws UnsupportedEncodingException{
		//request.setCharacterEncoding("UTF-8");
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		String specName = new String(request.getParameter("specName"));
		String specValue = request.getParameter("specValue");
		paramMap.put("specName",specName);
		paramMap.put("specValue", specValue);
		try {
			PageBean pageBean = productSpecService.listProductSpecPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询商品规格失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author lilp
 	 * @date 2017-08-29 14:31:56
	 */
	@RequestMapping("/toAddProductSpec")
	public String toAddProductSpec(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "product/productSpec_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author lilp
	 * @throws UnsupportedEncodingException 
 	 * @date 2017-08-29 14:31:56
	 */
	@RequestMapping("/toUpdateProductSpec")
	public String toUpdateProductSpec(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		String specName = request.getParameter("specName");
		request.setAttribute("specName", specName);
		return "product/productSpec_form";
	}
	
	
	/**
	 * 编辑页面查询规格值列表
	 * @return string 跳转页面路径
	 * @author lilp
	 * @throws UnsupportedEncodingException 
 	 * @date 2017-08-29 14:31:56
	 */
	@RequestMapping("/listProductSpecValueList")
	@ResponseBody
	public JSONGrid listProductSpecValueList(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		String specName = request.getParameter("specName");
		request.setAttribute("specName", specName);
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("specName", specName);
		try {
			List<ProductSpecEntity> pseList = productSpecService.listProductSpec(param);
			request.setAttribute("pseList", pseList);
			return new JSONGrid(pseList, pseList.size()); 
		} catch (Exception e) {
			handleException("跳转编辑商品规格页面错误", logger, e);
		}
		return new JSONGrid();
	}
	
	


	/**
	 * 新增或保存商品规格
	 * @param request 请求对象
	 * @param productSpecEntity 商品规格实体
	 * @return
	 * @author lilp
	 * @throws UnsupportedEncodingException 
 	 * @date 2017-08-29 14:31:56
	 */
	@RequestMapping("/saveOrUpdateProductSpec")
	public String saveProductSpec(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		String specName = request.getParameter("specName");
		specName = StringUtil.isNotNull(specName) ? specName.trim() : specName;
		String[] specIdArr = request.getParameterValues("specId");
		String[] specValueArr = request.getParameterValues("specValue");
		String[] specPriceAttr = request.getParameterValues("specPrice");
		String[] statusAttr = request.getParameterValues("status");
		
		List<ProductSpecEntity> pseList = new ArrayList<ProductSpecEntity>();
		int i = 0;
		for (String specVlaue : specValueArr) {
			ProductSpecEntity pse = new ProductSpecEntity();
			if(StringUtil.isNotEmpty(specIdArr[i])){
				pse.setId(Long.parseLong(specIdArr[i]));
			}
			pse.setSpecName(specName);
			specVlaue = StringUtil.isNotNull(specVlaue) ? specVlaue.trim() : specVlaue;
			pse.setSpecValue(specVlaue);
			pse.setSpecPrice(new BigDecimal(specPriceAttr[i]));
			pse.setStatus(Integer.parseInt(statusAttr[i]));
			pse.setSortNo(i);
			pse.setCreater(getUserName());
			pse.setModifier(getUserName());
			pseList.add(pse);
			i++;
		}
		if(pseList !=null && pseList.size() > 0){
			productSpecService.updateProductSpec(specName, pseList);
		}
		return "redirect:/productSpec/toListProductSpecPage";
	}
	
	
	/**
	 * 修改商品规格配置排序值
	 * @param request 请求对象
	 * @return
	 * @author lilp
	 * @throws UnsupportedEncodingException 
 	 * @date 2017-08-29 14:31:56
	 */
	@RequestMapping("/updateProductSpecSort")
	@ResponseBody
	public Map<String, String> updateProductSpecSort(HttpServletRequest request) throws UnsupportedEncodingException {
		Map<String, String> resultMap = new HashMap<String, String>();
		request.setCharacterEncoding("UTF-8");
		String specName = request.getParameter("specName");
		String id = request.getParameter("id");
		String direction = request.getParameter("direction");
		try {
			// 修改排序值
			int result = productSpecService.updateProductSpecSort(specName, id, direction);
			resultMap.put("resultCode", result>0?"1":"0");
			resultMap.put("resultMsg", result>0?"操作成功":"操作失败");
		} catch (Exception e) {
			handleException("跳转编辑商品规格页面错误", logger, e);
			resultMap.put("code", "0");
			resultMap.put("resultMsg", "操作失败");
		}
		return resultMap;
	}
	
	
	/**
	 * 删除一组商品规格配置
	 * @param request 请求对象
	 * @return
	 * @author lilp
	 * @throws UnsupportedEncodingException 
 	 * @date 2017-08-29 14:31:56
	 */
	@RequestMapping("/deleteProductSpec")
	@ResponseBody
	public Map<String, String> deleteProductSpecByName(HttpServletRequest request) throws UnsupportedEncodingException {
		Map<String, String> resultMap = new HashMap<String, String>();
		request.setCharacterEncoding("UTF-8");
		String specName = request.getParameter("specName");
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("specName", specName);
			// 删除商品规格配置
			int result = productSpecService.deleteProductSpec(param);
			resultMap.put("resultCode", result>0?"1":"0");
			resultMap.put("resultMsg", result>0?"操作成功":"操作失败");
		} catch (Exception e) {
			handleException("删除一组商品规格配置", logger, e);
			resultMap.put("code", "0");
			resultMap.put("resultMsg", "操作失败");
		}
		return resultMap;
	}
	
	
	/**
	 * 删除商品规格值
	 * @param request 请求对象
	 * @return
	 * @author lilp
	 * @throws UnsupportedEncodingException 
 	 * @date 2017-08-29 14:31:56
	 */
	@RequestMapping("/deleteProductSpecValue")
	@ResponseBody
	public Map<String, String> deleteProductSpecValue(HttpServletRequest request) throws UnsupportedEncodingException {
		Map<String, String> resultMap = new HashMap<String, String>();
		request.setCharacterEncoding("UTF-8");
		String id = request.getParameter("id");
		try {
			int result = productSpecService.deleteProductSpecById(Long.parseLong(id));
			resultMap.put("resultCode", result>0?"1":"0");
			resultMap.put("resultMsg", result>0?"操作成功":"操作失败");
		} catch (Exception e) {
			handleException("删除商品规格值", logger, e);
			resultMap.put("code", "0");
			resultMap.put("resultMsg", "操作失败");
		}
		return resultMap;
	}
	
}
